﻿Imports System.Data.SqlServerCe
Imports Framework.Data

Namespace Backup
    Public Class StoredFiles
        Shared Sub New()
            Try
                Dim aSelectColumns As String() = {"Id", "JobId", "FilePath", "FileSize", "CreationTimeUtc", "LastWriteTimeUtc", "ContractId", "Version", "Attributes", "StoredAs"}
                ObjectPersister.DefineObject(GetType(Entities.Backup.StoredFile), "BackupStoredFiles", aSelectColumns)
            Catch ex As Exception
                Framework.Misc.HandleException(ex)
            End Try
        End Sub
        Private Sub New()
        End Sub

        Public Shared Function GetAllByJobIdAndFilePath(ByVal jobId As Integer, ByVal filePath As String) As Generic.List(Of Entities.Backup.StoredFile)
            Dim sqlClause As String = "JobId=@JobId AND FilePath=@FilePath"
            Dim sqlOrder As String = "Version DESC"
            Dim sqlPar1 As New SqlCeParameter("@JobId", jobId)
            Dim sqlPar2 As New SqlCeParameter("@FilePath", filePath)
            Return ObjectPersister.GetAll(Of Entities.Backup.StoredFile)(sqlClause, sqlOrder, sqlPar1, sqlPar2)
        End Function

        Public Shared Sub Save(ByVal file As Entities.Backup.StoredFile)
            Dim clsCmd As New SaveCommand(Of Long)("BackupStoredFiles", "Id", file.Id)
            clsCmd.AddParam("JobId", file.JobId, False)
            clsCmd.AddParam("FilePath", file.FilePath, False)
            clsCmd.AddParam("FileSize", file.FileSize, False)
            clsCmd.AddParam("CreatationTimeUtc", file.CreationTimeUtc, False)
            clsCmd.AddParam("LastWriteTimeUtc", file.LastWriteTimeUtc, False)
            clsCmd.AddParam("ContractId", file.ContractId, False)
            clsCmd.AddParam("Version", file.Version, False)
            clsCmd.AddParam("Attributes", CInt(file.Attributes), False)
            clsCmd.AddParam("StoredAs", file.StoredAs, False)
            clsCmd.Save()
        End Sub

        Public Shared Sub ResetTouched(ByVal jobId As Integer)
            Dim sqlCmd As Command = Environment.NewCommand("update BackupStoredFiles set Touched=0 where JobId=@JobId")
            sqlCmd.AddParameterWithValue("@JobId", jobId)
            sqlCmd.ExecuteNonQuery()
        End Sub
    End Class
End Namespace